package top.zhangjianyong.tools.service.api.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.zhangjianyong.tools.entity.TransactionOrder;
import top.zhangjianyong.tools.factory.TransactionOrderFactory;
import top.zhangjianyong.tools.parm.TransactionOrderAddParm;
import top.zhangjianyong.tools.parm.TransactionOrderPageParm;
import top.zhangjianyong.tools.parm.TransactionOrderUpdateParm;
import top.zhangjianyong.tools.service.api.TransactionOrderService;
import top.zhangjianyong.tools.service.mybatis.ITransactionOrderService;
import top.zhangjianyong.tools.utils.ApiResponse;

/**
 * @author ： zhangjianyong
 * @date ： 2022/4/25 2:12 下午
 */
@Service
public class TransactionOrderServiceImpl implements TransactionOrderService {
    @Autowired
    private ITransactionOrderService iTransactionOrderService;

    @Override
    public ApiResponse add(TransactionOrderAddParm transactionOrderAddParm) {
        TransactionOrder transactionOrder = TransactionOrderFactory.create(transactionOrderAddParm);

        iTransactionOrderService.save(transactionOrder);
        return ApiResponse.success();
    }

    @Override
    public ApiResponse update(Long id, TransactionOrderUpdateParm transactionOrderUpdateParm) {
        TransactionOrder transactionOrder = TransactionOrderFactory.create(id, transactionOrderUpdateParm);
        iTransactionOrderService.updateById(transactionOrder);
        return ApiResponse.success();
    }

    @Override
    public ApiResponse page(TransactionOrderPageParm transactionOrderPageParm, int page, int size) {

        IPage<TransactionOrder> iPage =
                iTransactionOrderService
                        .lambdaQuery()
                        .orderByDesc(TransactionOrder::getCreateTime)
                        .page(new Page<>(page, size));


        return ApiResponse.success(iPage);
    }
}
